package com.xxxx.yoyo.adminweb;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Order;
import com.xxxx.yoyo.service.IOrderService;
import com.xxxx.yoyo.service.impl.OrderServiceImpl;

/**
 * 后台宠物物流(修改订单状态)，未付款，已付款，完成，删除
 * Servlet implementation class OrderOperateServlet
 */
@WebServlet("/admin/orderOperate")
public class OrderOperateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IOrderService orderService = new OrderServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取前端传递过来的参数：operate,id,status
		String operate = request.getParameter("operate");
		String orderIdstr = request.getParameter("id");		//send表示要发货，finish表示要完成订单，delete表示要删除订单
		int orderId = Integer.parseInt (orderIdstr);
		String statustr = request.getParameter("status");	//0表示全部订单，1表示未付款，2表示已付款，3表示配送中，4表示已完成
		int status = Integer.parseInt(statustr);
		
		//根据订单id获取订单
		Order order = orderService.getOrderById(orderId);
		
		switch(operate){
		case "send"://点击的是[发货]按钮，说明订单状态需要变成配送中（订单状态为3）
			order.setStatus(3);
			break;
		case "finish"://点击的是[完成]按钮，说明订单状态需要变成已完成（订单状态为4）
			order.setStatus(4);
			break;
		case "delete"://点击的是[删除]按钮，说明订单状态需要变成已删除（订单状态为0）I
			order.setStatus(0);
			break;
		}
		//更新订单信息到数据库
		orderService.updateOrder(order);
		
		//根据status的值重定向到订单列表页面
		response.sendRedirect("/YoyoShop/admin/orderList?status="+ status +"&page=1&size=16");
	}

}
